package com.ititer.web.controller;

import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.util.CollectionUtils;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;

import com.ititer.annotation.LoginRequired;
import com.ititer.bean.AccountBean;
import com.ititer.bean.BasicSettingsBean;
import com.ititer.bean.TechSettingsBean;
import com.ititer.enums.CodeDictionaryTypes;
import com.ititer.enums.PublicKeys;
import com.ititer.model.AccountDomains;
import com.ititer.model.CodeDictionary;
import com.ititer.model.Profile;
import com.ititer.web.abs.AbstractController;
import com.ititer.web.module.Modules;

@Resource
public class Settings extends AbstractController {

	@Get("/settings")
	@LoginRequired
	public void index() {
		addData(PublicKeys.AvaliableModules.getValue(), Modules.basicSettings);
		addData(PublicKeys.AvaliableModules.getValue(), Modules.techSettings);
	}

	@Get("/settings/basic")
	@LoginRequired
	public void basic() {
		Profile profile = accountSvc.getProfile(getSession().getId());
		if (profile != null) {
			BasicSettingsBean bean = new BasicSettingsBean();
			bean.bindFrom(profile);
			putData("basic", bean);
		}
	}

	@Post("/settings/basic")
	@LoginRequired
	public void postBasic(BasicSettingsBean basic) {
		validate(basic).onErrorRedirectTo(Settings.class).index();
		Integer id = getSession().getId();
		basic.setAccountId(id);
		boolean b = accountSvc.updateProfile(basic, id);
		if (!b) {
			putData("basic", basic);
			setErrorMessage("save.data.failure");
		}
		controllerResult.redirectTo(Settings.class).index();
	}

	@Get("/settings/tech")
	@LoginRequired
	public void tech() {
		TechSettingsBean tech = new TechSettingsBean();
		List<CodeDictionary> domains = codeDictionarySvc.getCodeDictionary(CodeDictionaryTypes.ItDomain.getValue());
		tech.setAllDomains(domains);

		List<CodeDictionary> positions = codeDictionarySvc.getCodeDictionary(CodeDictionaryTypes.ItPosition.getValue());
		tech.setAllPositions(positions);

		Integer accountId = getSession().getId();
		List<AccountDomains> selectedDomain = accountSvc.getDomains(accountId);
		if (!CollectionUtils.isEmpty(selectedDomain)) {
			String[] domainNames = new String[selectedDomain.size()];
			int i = 0;
			for (AccountDomains domain : selectedDomain) {
				domainNames[i] = domain.getDomain();
				i++;
			}
			tech.setDomains(domainNames);
		}

		Profile profile = accountSvc.getProfile(accountId);
		if (profile != null) {
			tech.bindFrom(profile);
		}
		putData("tech", tech);
	}

	@Post("/settings/tech")
	@LoginRequired
	public void postTech(TechSettingsBean tech) {
		String[] domains = tech.getDomains();
		if (ArrayUtils.isEmpty(domains)) {
			setErrorMessage("please.select.domain");
			controllerResult.redirectTo(Settings.class).tech();
		}
		Integer id = getSession().getId();
		AccountBean account = new AccountBean();
		account.setId(id);
		account.setDomains(domains);
		accountSvc.updateDomain(account);
		accountSvc.updateProfile(tech, id);
		controllerResult.redirectTo(Settings.class).index();
	}
}
